import com.unknown.Role;
import com.unknown.User;
import com.unknown.UserRole
import com.unknown.world.Tile
import com.unknown.world.Room
import org.eclipse.jetty.util.log.Log
import org.eclipse.jetty.util.log.StdErrLog;

class BootStrap {

	def springSecurityService;
	
    def init = { servletContext ->
        Log.setLog(new StdErrLog(debugEnabled: true))

		if(!Role.count()){
			new Role(authority: "ROLE_ADMIN").save()
			new Role(authority: "ROLE_USER").save()
			new Role(authority: "ROLE_CREATOR").save()
		}
		if(!User.findByUsername('admin')){
            //Default Users
			def adminRole = Role.findByAuthority("ROLE_ADMIN")
			User admin = new User(username:"admin", password:"t", enabled:true);
			admin.save()
            new UserRole(user:admin, role:adminRole).save()

            //Default tiles
            ["n","ne","nw","s","se","sw"].each{
                new Tile(src: "tiles/hex_grass/out_${it}.png", name: "Grass (${it})", height: 50, width: 50, type: 'background').save();
            }
            new Tile(src: 'tiles/hex_blue/out_n.png', name: 'Blue', height: 50, width: 50, type: 'background').save();
            new Tile(src: 'tiles/hex_dirt/out_n.png', name: 'Dirt', height: 50, width: 50, type: 'background').save();
            new Tile(src: 'tiles/hex_grey/out_n.png', name: 'Grey', height: 50, width: 50, type: 'background').save();
            new Tile(src: 'tiles/hex_sand/out_n.png', name: 'Sand', height: 50, width: 50, type: 'background').save();

            //Default Room
            Room room = new Room(name: 'Main', entityId: '4dd85b36050615fbf8f54ffb');
            room.origin = room;
            room.save();
		}

    }
    def destroy = {
    }
}
